#encoding: utf-8

#this tool will generate crmeb clear sql , and clear db if using -f flag

load 'rds_api.rb'

def genProductsSqls
end

#select routine_name from information_schema.routines where routine_schema = 'crmeb';
#select * from information_schema.parameters where specific_schema = 'crmeb';
def genDropSqls
  sqls = []
  #sql = "SELECT CONCAT('drop table ',table_name,';') as sql FROM information_schema.`TABLES` WHERE table_schema='crmeb';"

  sql = "SELECT table_name FROM information_schema.`TABLES` WHERE table_schema='crmeb';"
  queryRds(sql).each { |r| sqls += [ "drop table crmeb.#{r['table_name']}; " ] }

  sql = "select routine_name from information_schema.routines where routine_schema = 'crmeb';"
  queryRds(sql).each { |r| sqls += [ "drop function crmeb.#{r['routine_name']}; " ] }

  return sqls

end

overwrite_mode = false
args = ''
ARGV.each { |arg| args+=arg }
xlsx=args if args!='' && args!='-f'
overwrite_mode=true if args=='-f'

puts "generating dop sqls for crmeb"
sqls = genDropSqls
File.open("0-clear-crmeb.sql","w:UTF-8") { |f| f.write sqls.join("\n")+"\n" }
puts "done"

if overwrite_mode
  puts "droping tables & functions.."
  commitTrans sqls
  puts "done"
end
